#coding=utf-8
#Boa:FramePanel:PanelCustomerLogList

import wx

import DlgCustomerChoice
import DlgPopupSupplier

from product import *
from ctrlAider import *
from MyListCtrl import *


[wxID_PANELCUSTOMERLOGLIST, wxID_PANELCUSTOMERLOGLISTBTNALLDATE, 
 wxID_PANELCUSTOMERLOGLISTBTNDAYAFTER, wxID_PANELCUSTOMERLOGLISTBTNDAYBEFORE, 
 wxID_PANELCUSTOMERLOGLISTBTNEXPORTLIST, wxID_PANELCUSTOMERLOGLISTBTNONEDAY, 
 wxID_PANELCUSTOMERLOGLISTBTNONEMONTH, wxID_PANELCUSTOMERLOGLISTBTNONEWEEK, 
 wxID_PANELCUSTOMERLOGLISTBTNONEYEAR, wxID_PANELCUSTOMERLOGLISTBTNPOPUPPEER, 
 wxID_PANELCUSTOMERLOGLISTBTNSEARCH, wxID_PANELCUSTOMERLOGLISTCBREGION, 
 wxID_PANELCUSTOMERLOGLISTDATEEND, wxID_PANELCUSTOMERLOGLISTDATESTART, 
 wxID_PANELCUSTOMERLOGLISTSTATICTEXT1, wxID_PANELCUSTOMERLOGLISTTCPEERNAME, 
 wxID_PANELCUSTOMERLOGLISTTDATE, wxID_PANELCUSTOMERLOGLISTTPEER, 
] = [wx.NewId() for _init_ctrls in range(18)]

[Type_Sale, Type_Pur] = [0,1]

class PanelCustomerLogList(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELCUSTOMERLOGLIST,
              name=u'PanelSaleList', parent=prnt, pos=wx.Point(713, 328),
              size=wx.Size(1059, 707), style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(1043, 669))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.TPeer = wx.StaticText(id=wxID_PANELCUSTOMERLOGLISTTPEER,
              label=u'\u5355\u4f4d\u540d\u79f0\uff1a', name=u'TPeer',
              parent=self, pos=wx.Point(22, 70), size=wx.Size(85, 19), style=0)

        self.TDate = wx.StaticText(id=wxID_PANELCUSTOMERLOGLISTTDATE,
              label=u'\u67e5\u8be2\u65e5\u671f\uff1a', name=u'TDate',
              parent=self, pos=wx.Point(22, 38), size=wx.Size(85, 19), style=0)
        self.TDate.SetAutoLayout(True)
        self.TDate.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'Arial'))

        self.dateStart = wx.DatePickerCtrl(id=wxID_PANELCUSTOMERLOGLISTDATESTART,
              name=u'dateStart', parent=self, pos=wx.Point(126, 30),
              size=wx.Size(120, 27), style=wx.DP_DROPDOWN)
        self.dateStart.SetLabel(u'2014-04-24')
        self.dateStart.SetValue(wx.DateTimeFromDMY(24, 3, 2014, 0, 0, 0))
        self.dateStart.SetToolTipString(u'dateStart')

        self.tcPeerName = wx.TextCtrl(id=wxID_PANELCUSTOMERLOGLISTTCPEERNAME,
              name=u'tcPeerName', parent=self, pos=wx.Point(126, 64),
              size=wx.Size(250, 28), style=0, value=u'')

        self.staticText1 = wx.StaticText(id=wxID_PANELCUSTOMERLOGLISTSTATICTEXT1,
              label=u'\u81f3', name='staticText1', parent=self,
              pos=wx.Point(254, 38), size=wx.Size(18, 18), style=0)

        self.dateEnd = wx.DatePickerCtrl(id=wxID_PANELCUSTOMERLOGLISTDATEEND,
              name=u'dateEnd', parent=self, pos=wx.Point(278, 30),
              size=wx.Size(116, 27), style=wx.DP_DROPDOWN)

        self.btnOneDay = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNONEDAY,
              label=u'\u5f53\u65e5', name=u'btnOneDay', parent=self,
              pos=wx.Point(402, 30), size=wx.Size(46, 29), style=0)
        self.btnOneDay.Bind(wx.EVT_BUTTON, self.OnBtnOneDayButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNONEDAY)

        self.btnOneWeek = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNONEWEEK,
              label=u'\u4e00\u5468', name=u'btnOneWeek', parent=self,
              pos=wx.Point(450, 30), size=wx.Size(46, 29), style=0)
        self.btnOneWeek.Bind(wx.EVT_BUTTON, self.OnBtnOneWeekButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNONEWEEK)

        self.btnOneMonth = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNONEMONTH,
              label=u'\u4e00\u6708', name=u'btnOneMonth', parent=self,
              pos=wx.Point(498, 30), size=wx.Size(46, 29), style=0)
        self.btnOneMonth.Bind(wx.EVT_BUTTON, self.OnBtnOneMonthButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNONEMONTH)

        self.btnOneYear = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNONEYEAR,
              label=u'\u4e00\u5e74', name=u'btnOneYear', parent=self,
              pos=wx.Point(546, 30), size=wx.Size(46, 29), style=0)
        self.btnOneYear.Bind(wx.EVT_BUTTON, self.OnBtnOneYearButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNONEYEAR)

        self.btnAllDate = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNALLDATE,
              label=u'\u5168\u90e8', name=u'btnAllDate', parent=self,
              pos=wx.Point(594, 30), size=wx.Size(46, 29), style=0)
        self.btnAllDate.Bind(wx.EVT_BUTTON, self.OnBtnAllDateButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNALLDATE)

        self.btnSearch = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNSEARCH,
              label=u'\u67e5\u8be2', name=u'btnSearch', parent=self,
              pos=wx.Point(408, 64), size=wx.Size(72, 29), style=0)
        self.btnSearch.Bind(wx.EVT_BUTTON, self.OnBtnSearchButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNSEARCH)

        self.btnPopupPeer = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNPOPUPPEER,
              label=u'+', name=u'btnPopupPeer', parent=self, pos=wx.Point(376,
              64), size=wx.Size(24, 29), style=0)
        self.btnPopupPeer.Bind(wx.EVT_BUTTON, self.OnBtnPopupPeerButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNPOPUPPEER)

        self.btnExportList = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNEXPORTLIST,
              label=u'\u5bfc\u51fa\u5f53\u524d\u6570\u636e\u5230EXCEL',
              name=u'btnExportList', parent=self, pos=wx.Point(579, 64),
              size=wx.Size(200, 29), style=0)
        self.btnExportList.Bind(wx.EVT_BUTTON, self.OnBtnExportListButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNEXPORTLIST)

        self.btnDayBefore = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNDAYBEFORE,
              label=u'\u524d\u4e00\u5929', name=u'btnDayBefore', parent=self,
              pos=wx.Point(656, 30), size=wx.Size(64, 29), style=0)
        self.btnDayBefore.Bind(wx.EVT_BUTTON, self.OnBtnDayBeforeButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNDAYBEFORE)

        self.btnDayAfter = wx.Button(id=wxID_PANELCUSTOMERLOGLISTBTNDAYAFTER,
              label=u'\u540e\u4e00\u5929', name=u'btnDayAfter', parent=self,
              pos=wx.Point(720, 30), size=wx.Size(64, 29), style=0)
        self.btnDayAfter.Bind(wx.EVT_BUTTON, self.OnBtnDayAfterButton,
              id=wxID_PANELCUSTOMERLOGLISTBTNDAYAFTER)

        self.cbRegion = wx.ComboBox(choices=[],
              id=wxID_PANELCUSTOMERLOGLISTCBREGION, name=u'cbRegion',
              parent=self, pos=wx.Point(872, 64), size=wx.Size(145, 27),
              style=0, value=u'')
        self.cbRegion.SetLabel(u'')
        self.cbRegion.Show(False)
        self.cbRegion.Bind(wx.EVT_COMBOBOX, self.OnCbRegionCombobox,
              id=wxID_PANELCUSTOMERLOGLISTCBREGION)

    def __init__(self, parent, type):
        self._init_ctrls(parent)
        
        self.type = type
        self.myInit()
   
               
    def myInit(self):
        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())
        
        self.m_peer = None
        self.tcPeerName.SetFocus()
        
        if(self.type == Type_Sale):
            #self.TPeer.SetLabel(u'客户名称')
            self.popupWin = DlgCustomerChoice.create(self)
            self.lcBill = MyListCtrlCustomerLog(self)
            
        elif(self.type == Type_Pur):
            #self.popupWin = DlgPopupSupplier.create(self)
            #self.lcBill = MyListCtrlPurOrder(self)
            pass
        
        self.tcPeer = MyPopupTextCtrlDecorator(self.tcPeerName, self.popupWin)
        
        self.lcBill.SetSize(wx.Size(1000, 500))
        self.lcBill.Move(wx.Point(22,100))
        
        
    def setCustomer(self, obj):
        self.setSelectPeer(obj)
        
    def setSelectObj(self, obj):
        self.setSelectPeer(obj)
            
    def setSelectPeer(self, obj):
        if (obj is not None): 
            self.m_peer = obj
            self.tcPeer.ChangeValue(obj.getValue(obj.COL_NAME))
            
            self.dateEnd.SetValue(wx.DateTime.Today())        
            self.dateStart.SetValue(wx.DateTimeFromDMY(1,0,2000,0,0,00))
            self.RunSearch()
        
    def getSelectPeer(self):
        return self.m_peer 
                                        
        
    def OnBtnOneDayButton(self, event):
        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())

    def OnBtnOneWeekButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Days(7)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)
        

    def OnBtnOneMonthButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Months(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)

    def OnBtnOneYearButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Years(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)

    def OnBtnAllDateButton(self, event):
        self.dateEnd.SetValue(wx.DateTime.Today())        
        self.dateStart.SetValue(wx.DateTimeFromDMY(1,0,2000,0,0,00))

    def OnBtnDayBeforeButton(self, event):
        day = self.dateEnd.GetValue()        
        span = wx.DateSpan.Days(1)
        day.SubtractDS(span)
        self.dateEnd.SetValue(day)
        self.dateStart.SetValue(day)

    def OnBtnDayAfterButton(self, event):
        day = self.dateEnd.GetValue()        
        span = wx.DateSpan.Days(1)
        day.AddDS(span)
        self.dateEnd.SetValue(day)
        self.dateStart.SetValue(day)
        
    def OnBtnSearchButton(self, event):
        self.RunSearch()
    
    def RunSearch(self):
        '''按照搜索条件，显示订单信息'''
        if self.tcPeer.GetValue() == '':
            #self.tcPeer.ChangeValue(u'全部客户')
            self.m_peer = None
            
            # 时间跨度大应提示时间长
            span = self.dateEnd.GetValue() - self.dateStart.GetValue()
            if span.GetDays() > 31:
                msg = u'没有选择单位，时间跨度较大，可能比较耗时，是否确认查询？'
                dlg = wx.MessageDialog(self, msg, u'提示消息', wx.YES_NO|wx.ICON_QUESTION)
                if dlg.ShowModal() != wx.ID_YES:
                    return
            
        filter = {'dateStart':self.dateStart.GetValue().Format('%Y-%m-%d'),
                    'dateEnd':self.dateEnd.GetValue().Format('%Y-%m-%d')}
                
        if self.m_peer is not None:
            filter['peerId'] = self.m_peer.getValue(self.m_peer.COL_ID)
            filter['peerName'] = self.m_peer.getValue(self.m_peer.COL_NAME)
                            
        cnt = self.lcBill.refresh(filter)
        if (cnt == 0):
            dlg = wx.MessageDialog(self, u'没有符合条件的记录', u'提示消息', wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
   
    def OnBtnPopupPeerButton(self, event):
        self.tcPeer.resetPopupWinPos()
        self.popupWin.Show() 

    def OnBtnExportListButton(self, event):
        dlg = wx.TextEntryDialog(self, u'请输入文件名，注意如果当前目录下存在同名文件，该同名文件将被覆盖',
                                u'提示',u'销售单.xls',wx.OK|wx.CANCEL|wx.CENTRE)
        if dlg.ShowModal() != wx.ID_OK:
            return
        file = dlg.GetValue()
        
        if(kit.exportList(self.lcBill, file) < 0):
            msg = u'数据导出失败，请查看是不是其他程序中打开了该文件！'
        else:
            msg = u'数据完成导出'
            
        dlg = wx.MessageDialog(self, msg, u'提示',wx.OK|wx.ICON_INFORMATION)
        dlg.ShowModal()

    def search_by_peer(self, peerObj):
        self.setSelectObj(peerObj)
        self.OnBtnOneYearButton(None)
        self.OnBtnSearchButton(None)

    def OnCbRegionCombobox(self, event):
        event.Skip()

    
    
